<?php
/*! \page license
 *
 * Copyright (c) 2011, Blaise Lengrand
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list
 * of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 * Neither the name of `snWin' nor the names of its contributors may be used to
 * endorse or promote products derived from this software without specific prior
 * written permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

  // Initialize the environement
  require_once("../feed.php");

  require_once("system/pages/social/rss/simplepie.inc");

  function feed_decode_url($str)
  {
    return html_entity_decode(urldecode($str));
  }

  function feed_decode_html($str)
  {
    return urldecode($str);
  }

  /**
   * Create and import a feed from a URL
   */
  function feed_import($url)
  {
    global $_CURRENT;

    $sp_feed = new SimplePie();
    $sp_feed->set_feed_url($url);

    // Sort by date
    $sp_feed->enable_order_by_date(true);
    // Enable the discovering and caching of favicons.
    $sp_feed->set_favicon_handler('system/pages/social/rss/handler_image.php');
    // Increase the timeout
    $sp_feed->set_timeout(60);
    // Make sure the cache is enabled
    $sp_feed->enable_cache(true);
    // Set cache location
    $sp_feed->set_cache_location("./.cache");
    // Set the cache validity (in seconds)
    $sp_feed->set_cache_duration(60);
    // Init the feed
    $sp_feed->init();
    // We'll make sure that the right content type and character encoding gets set automatically.
    // This function will grab the proper character encoding, as well as set the content type to text/html.
    $sp_feed->handle_content_type();

    if ($sp_feed->error())
      feed_json_error($sp_feed->error());

    // Set the picture picture
    $picture = urldecode($sp_feed->get_favicon());
    if (!$picture)
      $picture = 'system/pages/social/rss/rss.png';

    // Initianlize the array which will contain the items
    $item = array();

    foreach ($sp_feed->get_items() as $sp_item)
    {
      $link = feed_decode_url($sp_item->get_permalink());
      $item[] = array(
        "id" => $sp_item->get_id(true),
        "name" => "<a href=\"".$link."\">".feed_decode_html($sp_item->get_title())."</a>",
        "picture" => "system/pages/social/rss/rss.png",
        "link" => $link,
        "message" => feed_decode_html($sp_item->get_content()),
        "date" => strtotime($sp_item->get_date('j F Y G:i:s')),
        "icon" => feed_decode_url($picture)
      );
    }

    feed_json_success(array_reverse($item));
  }

  if (!isset($_GET['url']) || !$_GET['url'])
    feed_json_error("Invalid feed URL");

  // Strip slashes if magic quotes is enabled (which automatically escapes certain characters)
  if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
    $_GET['url'] = stripslashes($_GET['url']);

  feed_import($_GET['url']);
?>
